package com.dut.bbs.activity;

import java.util.Map;

import android.R.integer;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.dut.bbs.R;
import com.dut.bbs.activity.ArticleActivity.HtmlSourseGeter;
import com.dut.bbs.adapter.ArtlistAdapter;
import com.dut.bbs.data.ParseArticle;
import com.dut.bbs.data.ParseMailDetail;
import com.dut.bbs.entity.MailEntity;
import com.dut.bbs.util.Dg;
import com.dut.bbs.util.URLConstant;
import com.dut.bbs.web.HtmlSource;

public class MailDetailActivity extends BaseActivity {
	private TextView title,author,date,content;
	private Button bt_reply,bt_del;
	private Context context;
	private String delurl;
	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) { 
			switch(msg.what){
			case 1:
				Dg.showToast(context, "删除成功！");
//				Intent intent = new Intent(context,MailActivity.class);
//				startActivity(intent);				
				MailDetailActivity.this.finish();
				getProgressDialog().dismiss();
				break;
			case 0:
				Dg.showToast(context, "删除失败！");
				getProgressDialog().dismiss();
				break;
			}
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mail_detail);
		TextView tv_title = (TextView) findViewById(R.id.activitytitle);
		tv_title.setText("阅读邮件");
		context = this;
		String url = getIntent().getStringExtra("url");
		title = (TextView) findViewById(R.id.tv_mail_title);
		author = (TextView) findViewById(R.id.tv_mail_author);
		date = (TextView) findViewById(R.id.tv_mail_time);
		content = (TextView) findViewById(R.id.tv_mail_content);
//		content.setMovementMethod(ScrollingMovementMethod.getInstance());
		bt_reply = (Button) findViewById(R.id.btn_mail_reply);
		bt_del = (Button) findViewById(R.id.btn_mail_delete);
		bt_reply.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(context,PostActivity.class);
				String id = author.getText().toString();
				String retitle = "Re: "+title.getText().toString();
				Bundle bundle = new Bundle();
				bundle.putString("id", id);
				bundle.putString("title", retitle);
				bundle.putInt("flag", 5);
				intent.putExtras(bundle);
				startActivity(intent);
			}
		});
		bt_del.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				getProgressDialog().setMessage("删除邮件中...");
	            getProgressDialog().show();
	            new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						boolean flag = MailEntity.delmail(delurl);
						if(flag)
							handler.sendEmptyMessage(1);
						else
							handler.sendEmptyMessage(0);
					}
				}).run();
			}
		});
		HtmlSourseGeter h = new HtmlSourseGeter(context);
		h.execute(URLConstant.URL_BASE+url);
	}
	class HtmlSourseGeter extends AsyncTask<String, integer, String>
    {

        private Context context;
        public HtmlSourseGeter(Context context)
        {
            this.context = context;
        }

        @Override
        protected String doInBackground(String... params)
        {
            String result = null;
            try
            {
                result = HtmlSource.getHtml_data(params[0]);
            } catch (Exception e)
            {
            }
            return result;
        }

        @Override
        protected void onPreExecute()
        {
        	getProgressDialog().setMessage("抓取页面中...");
            getProgressDialog().show();
        }

        @Override
        protected void onPostExecute(String str)
        {

            if (str == null)
            {
                Dg.showToast(context, "抓取失败！");
                getProgressDialog().dismiss();
                return;
            }
            try{
            	Map<String,String> map = ParseMailDetail.getMailDetail(str);
            	delurl = map.get("del");
                title.setText("标题："+map.get("title").trim().substring(3));
                author.setText("发信人："+map.get("author"));
                date.setText("发信于："+map.get("date"));
                content.setText(Html.fromHtml(map.get("content")));
                getProgressDialog().dismiss();
            }catch(Exception e){
            	Dg.showToast(context, "该邮件已删除！");
                getProgressDialog().dismiss();
                MailDetailActivity.this.finish();
            }               
        }

    }
}
